layui.config({
    base: basePath,
    version: skyeyeVersion
}).extend({
    window: 'js/winui.window'
}).define(['window', 'jquery', 'winui'], function (exports) {
    winui.renderColor();
    var $ = layui.$;

    // 从父页面获取传递的参数
    var intercourseTime = parent.intercourseTime || "";
    var keepfiStoreId = parent.keepfiStoreId || "";
    var mealByStoreId = parent.mealByStoreId || "";

    // 注册Handlebars辅助函数
    Handlebars.registerHelper('getStateName', function(state) {
        return skyeyeClassEnumUtil.getEnumDataNameByCodeAndKey("keepFitOrderState", 'id', state, 'name');
    });

    Handlebars.registerHelper('getStateClass', function(state) {
        // 根据状态返回对应的CSS类
        if (state == 1) {
            return 'state-down'; // 保养中
        } else if (state == 2) {
            return 'state-up'; // 待核销
        } else if (state == 3) {
            return 'state-up'; // 已核销
        } else {
            return '';
        }
    });

    Handlebars.registerHelper('getUserTypeName', function(userType) {
        return skyeyeClassEnumUtil.getEnumDataNameByCodeAndKey("keepFitOrderUserType", 'id', userType, 'name');
    });

    // 加载订单详情
    function loadOrderDetails() {
        try {
            var date = intercourseTime ? intercourseTime.split(' ')[0] : '';
            var params = {
                mealStoreId: mealByStoreId,
                keepFitStoreId: keepfiStoreId,
                date: date
            };
            Object.keys(params).forEach(key => {
                if (!params[key]) delete params[key];
            });

            AjaxPostUtil.request({
                url: shopBasePath + 'queryListByStoreIdsAndDate',
                params: params,
                type: 'json',
                method: 'POST',
                callback: function (json) {
                    var rows = json.rows || [];
                    // 计算总计
                    var totalPayable = 0, totalPay = 0;
                    rows.forEach(function(item) {
                        totalPayable += parseFloat(item.payablePrice) || 0;
                        totalPay += parseFloat(item.payPrice) || 0;
                    });
                    // 总计行
                    var totalRow = {
                        isTotal: true,
                        payablePrice: totalPayable.toFixed(2),
                        payPrice: totalPay.toFixed(2),
                    };
                    // 总计行放首行
                    var tableData = [totalRow].concat(rows);

                    var template = Handlebars.compile($("#orderTableTemplate").html());
                    var html = template(tableData);
                    $("#orderDetailContent").html(html);
                },
                error: function() {
                    $("#orderDetailContent").html(
                        '<div style="text-align: center; padding: 50px 0;">' +
                        '<i class="layui-icon layui-icon-face-cry" style="font-size: 50px; color: #ccc;"></i>' +
                        '<p style="margin-top: 10px; color: #999;">获取数据失败，请稍后重试</p>' +
                        '</div>'
                    );
                }
            });

        } catch (error) {
            $("#orderDetailContent").html(
                '<div style="text-align: center; padding: 50px 0;">' +
                '<i class="layui-icon layui-icon-face-cry" style="font-size: 50px; color: #ccc;"></i>' +
                '<p style="margin-top: 10px; color: #999;">加载失败: ' + (error.message || '未知错误') + '</p>' +
                '</div>'
            );
        }
    }

    // 页面加载完成后执行
    $(document).ready(function() {
        matchingLanguage();
        // 延迟执行以确保所有资源都已加载
        setTimeout(function() {
            loadOrderDetails();
        }, 100);
    });

    exports('dayKeepFitOrderList', {});
});
